import 'package:flutter/material.dart';
import 'package:aio/aio.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/manager/audit_manager.dart';
import 'package:app/src/presenter/review_presenter.dart';
import 'package:app/src/ui/app_ui.dart';
import 'package:app/src/ui/route/works/public/items/ui_attachment.dart';
import 'package:app/src/ui/route/works/public/items/ui_base.dart';
import 'package:app/src/ui/route/works/public/items/ui_contractInfo.dart';
import 'package:app/src/ui/route/works/public/items/ui_customersInfo.dart';
import 'package:app/src/ui/route/works/public/items/ui_new_customer.dart';
import 'package:app/src/ui/route/works/public/items/ui_node_list.dart';
import 'package:app/src/ui/route/works/public/items/ui_note.dart';
import 'package:app/src/ui/route/works/public/items/ui_payment.dart';
import 'package:app/src/ui/route/works/public/acom/ui_product.dart';
import 'package:app/src/ui/route/works/public/items/ui_specialclauses.dart';
import 'package:app/src/ui/route/works/public/review_details_ui.dart';
import 'package:app/src/ui/route/works/public/review_submitted.dart';

import '../../../../http/bean/ywx_limit.dart';
import '../../../dialog/dailog.dart';
import '../public/image/ui_image.dart';
import '../public/image/ui_image_upload.dart';
import '../public/items/ui_single.dart';
import 'acom/ui_report_loss_adapter1.dart';
import 'acom/ui_report_loss_adapter2.dart';
import 'item/ui_report_loss.dart';

///报损审核详情
class ReportLossDetailsRoute extends StatefulWidget {
  YwxItem item;

  ReportLossDetailsRoute(this.item);

  @override
  _State createState() => _State();
}

class _State extends State<ReportLossDetailsRoute> {
  _Presenter presenter = _Presenter();

  @override
  void initState() {
    super.initState();
    presenter.getContractapplygetinfoWithDelay(context); //获取审核详情数据
  }

  @override
  Widget build(BuildContext context) {
    return ReviewDetailsUi.build(context, presenter, _body);
  }

  Widget _body(BuildContext context) {
    if (presenter?.mDatas == null) {
      return AppUi.getEmptyView();
    }
    var mDatas = presenter?.mDatas?.FLoseApplyInfo;
    var mOldDatas = presenter?.mOldDatas?.FLoseApplyInfo;
    List<Widget> children = [];

    children.add(UiRportLoss.build(context, mDatas, mOldDatas)); //基本信息
    children.add(UiImage.build(context, mDatas?.getPics())); //图片证据

    var data = mDatas;
    var isReturnsPatch = false;
    if (data?.isReturnsPatch() ?? false) {
      //报损补片
      isReturnsPatch = true;
      //入库仓库
      var StorehouseList = data?.StorehouseList;
      if (StringUtils.isNotEmpty(StorehouseList)) {
        if (data?.Storehouse == null) {
          data?.Storehouse = data?.getDefaultStorehouse();
        }
        var builder = StatefulBuilder(
            builder: (BuildContext context, StateSetter setState) {
          var Storehouse = data?.Storehouse;
          return UiSingle.build(context, data?.Storehouse?.StorehouseName,
              data?.Storehouse?.StorehouseName,
              title: AppStrings.of(context).StorehouseList,
              isBlue: true, onTap: () {
            AppDialog.showDialogWithRollerPicker(context,
                    datas: StorehouseList, data: Storehouse)
                ?.then((value) {
              if (value != null) {
                setState(() {
                  data?.Storehouse = value;
                });
              }
            });
          });
        });
        children.add(builder);
      }
      //检验员
      var CheckerList = data?.LoseApplyUserInfo;
      if (StringUtils.isNotEmpty(CheckerList)) {
        if (data?.Checker == null) {
          data?.Checker = data.getDefaultChecker();
        }
        var builder = StatefulBuilder(
            builder: (BuildContext context, StateSetter setState) {
          var check = data?.Checker;
          return UiSingle.build(
              context, data?.Checker?.PersonName, data?.Checker?.PersonName,
              title: AppStrings.of(context).CheckerList,
              isBlue: true, onTap: () {
            AppDialog.showDialogWithRollerPicker(context,
                    datas: CheckerList, data: check)
                ?.then((value) {
              if (value != null) {
                setState(() {
                  data?.Checker = value;
                });
              }
            });
          });
        });
        children.add(builder);
      }
    }
    bool onlyBack = false; //什么也没有，只有返回键
    var hasBack = false; //退回选项
    var hasEnd = false;
    var hasAuditOpinion = true; //审核意见
    var agreeText = AppStrings.of(context).agree; //同意
    var endText = AppStrings.of(context).end; //终止
    if (isReturnsPatch) {
      //报损补片
      hasBack = false;
      hasAuditOpinion = false;
      children.add(UiReportLossAdapter1.build(
          context, data, data?.LoseApplyDetailN6081)); //报损补片
    } else {
      //报损审核
      hasBack = true;
      children.add(
          UiReportLossAdapter2.build(context, data?.LoseApplyDetail)); //报损审核明细
    }

    ///报损申请
    if (data?.isN6012() ?? false) {
      hasBack = false;
      hasEnd = true; //没有退回，只有终止。
      agreeText = AppStrings.of(context).Continuetoapply; //继续申请
      endText = AppStrings.of(context).Cancellationofapplication; //取消申请
      var id = "${data?.YwxId}${data?.Applicationcode}";
      children.add(UiImageUpLoad.build(context, id)); //上传证据
    }
    children.add(UiNoteList.build(context, presenter?.mDatas?.NodeList)); //审核日志
    return Column(
      children: [
        Expanded(child: UiBase.listView(context, children)),
        Visibility(
            child: ReviewSubmittedStateful(
              presenter?.mDatas,
              hasBack: hasBack,
              onlyBack: onlyBack,
              hasEnd: hasEnd,
              hasAuditOpinion: hasAuditOpinion,
              agreeText: agreeText,
              endText: endText,
              callbackWithT: (subData) {
                //fixme 提交参数数据
                // print(
                //     "========================================================入库仓位：${mDatas?.Storehouse}");
                // print(
                //     "========================================================检验员：${mDatas?.Checker}");
                subData.FLoseApplyInfo = mDatas;
                // print(
                //     "========================================================入库仓位：${subData.FLoseApplyInfo?.Storehouse}");
                // print(
                //     "========================================================检验员：${subData.FLoseApplyInfo?.Checker}");
              },
            ),
            visible: presenter?.mDatas != null)
      ],
    );
  }
}

class _Presenter extends ReviewPresenter {}
